package nl.nhl.idp.Views;

import java.io.IOException;
import nl.nhl.idp.Components.MainMenuItem;
import nl.nhl.idp.Components.MenuDragListener;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.Paint.Align;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class MainMenuView extends View
{
	private MenuDragListener listener;
	
	private Bitmap background;
	private Bitmap cursor;
	private float startX = 0;
	private float startY = 0;
	private float drawX, drawY;
	
	private String text = "";
	private Paint textPaint;
	
	public void setOnDragListener(MenuDragListener listener)
	{
		this.listener = listener;
	}
	
	public MainMenuView(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		if(!this.isInEditMode())
		{
			try
			{
				background = BitmapFactory.decodeStream(context.getAssets().open("images/mainmenu.jpg"));
				cursor = BitmapFactory.decodeStream(context.getAssets().open("images/mainstick.png"));
			}
			catch(IOException ioe){}
			
			textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
			textPaint.setTextSize(50);
			textPaint.setTextAlign(Align.CENTER);
			textPaint.setTypeface(Typeface.SANS_SERIF);
			textPaint.setColor(Color.WHITE);
			textPaint.setShadowLayer(5, 0, 0, Color.BLACK);
		}
	}

	public MainMenuView(Context context, AttributeSet attrs)
	{
		this(context, attrs, 0);
	}

	public MainMenuView(Context context)
	{
		this(context, null);
	}
	
	public void setText(String text)
	{
		this.text = text;
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh)
	{
		// TODO Auto-generated method stub
		super.onSizeChanged(w, h, oldw, oldh);
		if(background != null)
			background = Bitmap.createScaledBitmap(background, w, h, false);
		
		drawX = this.getWidth()/2;
		drawY = this.getHeight()/2;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		float deltaX = event.getX() - (this.getWidth() / 2);
		float deltaY = event.getY() - (this.getHeight() / 2);
		int length = (int)Math.sqrt(deltaX * deltaX + deltaY * deltaY);
		int angle = (int) (Math.atan2(deltaY, deltaX) * 180 / Math.PI);
		
		if(angle < 0)
			angle += 360;
		
		switch(event.getAction())
		{
			case MotionEvent.ACTION_DOWN:
			{
				startX = this.getWidth()/2;
				startY= this.getHeight()/2;
			}break;
			
			case MotionEvent.ACTION_MOVE:
			{
				// get the draw position
				drawX = startX + deltaX;
				drawY = startY + deltaY;
				
				DragProcessor(0, angle, length);
				
				invalidate();
				
			}break;
			
			case MotionEvent.ACTION_UP:
			{
				drawX = this.getWidth() / 2;
				drawY = this.getHeight() / 2;
				text = "";
				
				DragProcessor(1, angle, length);
				
				invalidate();

			}break;
		}
		
		return true;
	}
	
	/**
	 * Processes the given drag coordinates and performs actions based on the result
	 * @param touchAction 0=down, 1=up
	 * @param angle
	 * @param length
	 */
	private void DragProcessor(int touchAction, float angle, float length)
	{
		if(angle > 325 && angle < 355)
		{
			if(length > 310)
			{
				if(touchAction == 1)
				{
					if(listener != null)
						listener.onMenuItemSelected(MainMenuItem.Settings);
				}
				else
				{
					setText("Settings");
				}
			}
			else
			{
				setText("");
			}
		}
		
		//replay menuitem
		if(angle > 30 && angle < 55)
		{
			if(length > 200)
			{
				if(touchAction == 1)
				{
					if(listener != null)
						listener.onMenuItemSelected(MainMenuItem.Replay);
				}
				else
				{
					setText("Replay");
				}
			}
			else
			{
				setText("");
			}
		}
		
		//brute force menuitem
		if(angle > 115 && angle < 160)
		{
			if(length > 200)
			{
				if(touchAction == 1)
				{
					if(listener != null)
						listener.onMenuItemSelected(MainMenuItem.BruteForce);
				}
				else
				{
					setText("Brute Force");
				}
			}
			else
			{
				setText("");
			}
		}
		
		//own vehicle menuitem
		if(angle > 189 && angle < 210)
		{
			if(length > 335)
			{
				if(touchAction == 1)
				{
					if(listener != null)
						listener.onMenuItemSelected(MainMenuItem.OwnVehicle);
				}
				else
				{
					setText("Own Vehicle");
				}
			}
			else
			{
				setText("");
			}
		}
	}
	

	@Override
	protected void onDraw(Canvas canvas)
	{
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		if(!this.isInEditMode())
		{
			canvas.drawBitmap(background, 0, 0, new Paint());
			canvas.drawBitmap(cursor, drawX-cursor.getWidth() / 2, drawY-cursor.getHeight() / 2, new Paint());
			
			canvas.drawText(text, this.getWidth() / 2, this.getHeight() / 2, textPaint);
		}
	}
}